package sea.plot;

import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import sea.util.PublicParam;
import sea.util.WinHelper;
import simhya.script.ExploredSymbol;

/**
 *
 * This class builds the layout of Fix form interface
 * 
 * @author  Zhirun Ding
 * 
 * Created on 05/07/2012
 * 
 */

public class PlotNewFixForm extends javax.swing.JDialog {
    
    /** Creates new form PlotNewFixForm */
    public PlotNewFixForm(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        paramsList = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        valuesList = new javax.swing.JList();
        confirmButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(sea.SimHyAGui.class).getContext().getResourceMap(PlotNewFixForm.class);
        setTitle(resourceMap.getString("PlotNewFixForm.title")); // NOI18N
        setName("PlotNewFixForm"); // NOI18N
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        paramsList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        paramsList.setName("paramsList"); // NOI18N
        paramsList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                paramsListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(paramsList);

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        valuesList.setName("valuesList"); // NOI18N
        jScrollPane2.setViewportView(valuesList);

        confirmButton.setText(resourceMap.getString("confirmButton.text")); // NOI18N
        confirmButton.setName("confirmButton"); // NOI18N
        confirmButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                confirmButtonActionPerformed(evt);
            }
        });

        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(76, 76, 76)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(40, 40, 40))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(207, Short.MAX_VALUE)
                .addComponent(cancelButton)
                .addGap(18, 18, 18)
                .addComponent(confirmButton)
                .addGap(44, 44, 44))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(confirmButton)
                    .addComponent(cancelButton))
                .addGap(19, 19, 19))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.setVisible(false);
}//GEN-LAST:event_cancelButtonActionPerformed

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        //get data variable
        String datavar = ((PlotForm)PublicParam.PlotForm).getCurrentDataVariable();
        evars = PublicParam.SimHyACommand.getExploredParameters(datavar);
        //list of explored param
        WinHelper.initListbox3(paramsList, evars);
        
        //ArrayList<Symbol> vars = PublicParam.SimHyACommand.getExplorableParameters(datavar);
        //remove  explored params
        removeExceptExploredParam();               
        
        //remove all of values list
        valuesList.removeAll();
    }//GEN-LAST:event_formComponentShown

    void removeExceptExploredParam() {
        String exceptParam = "";
        String selectedParam = "";
        if (PublicParam.PlotStatexpForm != null && PublicParam.PlotStatexpForm.isVisible()) {
            exceptParam = ((PlotStatexplForm)PublicParam.PlotStatexpForm).getExceptExploredParam();
            selectedParam = ((PlotStatexplForm)PublicParam.PlotStatexpForm).getSelectedFix();
        } else if (PublicParam.PlotDetermForm != null && PublicParam.PlotDetermForm.isVisible()) {
            exceptParam = ((PlotDetermForm)PublicParam.PlotDetermForm).getExceptExploredParam();
            selectedParam = ((PlotDetermForm)PublicParam.PlotDetermForm).getSelectedFix();
        } else if (PublicParam.PlotPhaseForm != null && PublicParam.PlotPhaseForm.isVisible()) {
            exceptParam = ((PlotPhaseForm)PublicParam.PlotPhaseForm).getExceptExploredParam();
            selectedParam = ((PlotPhaseForm)PublicParam.PlotPhaseForm).getSelectedFix();
        }
        
        //remove except explored param
        if (exceptParam != null && exceptParam.length() > 0) {
            for (int i = 0; i < paramsList.getModel().getSize(); i++) {
                if (paramsList.getModel().getElementAt(i).toString().endsWith(exceptParam)) {
                    ((DefaultListModel)paramsList.getModel()).remove(i);
                    break;
                }
            }
        }
        
        //set default value of explored param
        WinHelper.setListSelectItem(paramsList, selectedParam);
    }
    
    private void paramsListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_paramsListMouseClicked
        String paramName = paramsList.getSelectedValue().toString();
        //System.out.println(paramName);
        if (paramName.length() > 0) {
            valuesList.removeAll();
            for (int i = 0; i < evars.size(); i++) {
                //System.out.println(evars.get(i).name + ":" + evars.get(i).first + "," + evars.get(i).last + "," + evars.get(i).points);
                DefaultListModel lm = new DefaultListModel();
                if (evars.get(i).name.endsWith(paramName)) {
                    for (int j = 0; j < evars.get(i).points; j++) {
                        //range=[p1=1:10#10,p2=1:1e4#5log],p1=[1,2,3,4,5,6,7,8,9,10] and p2=[1,10,100,1000,10000]
                        if (!evars.get(i).logScale) {                            
                            //lm.add(j, new java.text.DecimalFormat("#.00").format(evars.get(i).first + evars.get(i).last / evars.get(i).points * j));
                            lm.add(j, Double.parseDouble(new java.text.DecimalFormat("#.0000").format(evars.get(i).first + (evars.get(i).last - evars.get(i).first) / (evars.get(i).points - 1) * j)));
                        } else {
                            lm.add(j, evars.get(i).first * Math.pow(10.0, j));
                        }
                    }
                    valuesList.setModel(lm);
                    //set default value
                    valuesList.setSelectedIndex(0);
                    break;
                }
            }
        }
    }//GEN-LAST:event_paramsListMouseClicked

    private void confirmButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_confirmButtonActionPerformed
        if (toFixListVariable() == null) {
            JOptionPane.showMessageDialog(this, "Please select explored param and values.");
        } else{
            this.setVisible(false);
        }
    }//GEN-LAST:event_confirmButtonActionPerformed
    
    public Object[] toFixListVariable() {
        if (paramsList.getSelectedValue() == null || valuesList.getSelectedValue() == null) {            
            return null;
        }
        String paramName = paramsList.getSelectedValue().toString() + "";
        String s = "";
        for (int i = 0; i < valuesList.getSelectedValues().length; i++) {
            if (i > 0) s += ",";
            s += valuesList.getSelectedValues()[i].toString();
        }        
        //System.out.println(s.toString());
        return new Object[]{true, paramName, s};
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                PlotNewFixForm dialog = new PlotNewFixForm(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton confirmButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList paramsList;
    private javax.swing.JList valuesList;
    // End of variables declaration//GEN-END:variables
    ArrayList<ExploredSymbol> evars;
    //String exceptParam = "";
}
